using System;
using System.Data;
using GeoWeb.Common;
using GeoWeb.Controls.WebGrid;
using GeoWeb.Demo.DataContext;
using GeoWeb.DataContext;

namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        DbUtity db = new DbUtity();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebGrid1_OnWebGridRefresh(object sender, WebGridRefreshEventArgs e)
        {
            string sort = string.Empty;
            if (e.SortName != "" && e.SortOrder != "")
            {
                sort = " order by " + e.SortName + " " + e.SortOrder;
            }

            int total = db.Db.Count<Country>(new WhereClip("1=1"));
            WebGrid1.DataTable = db.Db.FromSql(string.Format(@"select top {0} * from Country where UID not in 
                (select top {1} UID from Country where 1=1 {2}) {3}", e.PageSize, (e.PageIndex - 1) * e.PageSize, sort, sort), null).ToTable() as DataTable;
            WebGrid1.RowCount = total;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebGrid1_OnWebGridColumnCreated(object sender, WebGridColumnCreatedEventArgs e)
        {
            if (e.TableType == TableTypes.CenterTable)
            {
                e.Column.Width = 200;
                if (e.Column is WebGridDataColumn)
                {
                    ((WebGridDataColumn)e.Column).EditAble = true;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebGrid1_WebGridCellRendering(object sender, WebGridCellRenderingEventArgs e)
        {
            if (e.Column.Type == ColumnTypes.Data && e.RowType == RowTypes.Data)
            {
                WebGridDataColumn datacol = e.Column as WebGridDataColumn;
                if (datacol.Field == "Name")
                {
                    e.FormatString = "<span style='color:red;'>" + e.Row[datacol.Field].ToString() + "</span>";
                    e.IsFormat = true;
                }
                if (e.RowIndex == 7)
                {
                    e.FormatString = "<span style='color:blue;font-weight:bold;'>" + e.Row[datacol.Field].ToString() + "</span>";
                    e.IsFormat = true;
                }
            }

            if (e.RowType == RowTypes.Header)
            {
                e.FormatString = "<span style='font-weight:bold;'>" + e.Column.Title + "</span>";
                e.IsFormat = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebGrid1_WebGridAfterCellEdit(object sender, WebGridAfterCellEditEventArgs e)
        {
            if (e.OldValue != e.NewValue)
            {
                string sqlstr = string.Format("update Country set {0}='{1}' where UID='{2}'", e.Field, e.NewValue, e.EditRow["UID"]);
                e.IsSuccess = db.Db.Update<Country>(new Field(e.Field), e.NewValue, Country._.UID == e.EditRow["UID"].ToString()) > 0;
            }
        }
    }
}
